<script setup lang="ts">
import { getDictTableData } from '@/api/dict/index'
import type { Page } from '@/types'
import type { DictTable } from '@/types/dict'
import { getFormatDictMap, dateFormat } from '@/util/dict'
import { defineAsyncComponent, onMounted, ref } from 'vue'
import { Search, Refresh } from '@/util/icons'
import Pagination from '@/components/Pagination.vue'
import AuthButton from '@/components/AuthButton.vue'
import AuthLink from '@/components/AuthLink.vue'
// 菜单名称
const pageN = '字典'
const page = ref<Page<DictTable>>({
  num: 1,
  size: 10,
  rows: [],
  count: 0
})
/* 获取对应的字典信息 */
const tableDict = ref<Map<string, string>>()
const statusDict = ref<Map<string, string>>()

// 页面加载
const loading = ref<boolean>(false)
// 获取页面数据
function getPageData() {
  loading.value = true
  getDictTableData(page.value, searchForm.value)
    .then((tabs) => {
      page.value = tabs.data
    })
    .finally(() => {
      setTimeout(() => {
        loading.value = false
      }, 500)
    })
}
onMounted(() => {
  getFormatDictMap('dict_manage', 'head').then((d) => {
    tableDict.value = d
  })
  getFormatDictMap('sys_dict_status', 'val').then((d) => {
    statusDict.value = d
  })
  getPageData()
})

/* 搜索 */
/* 是否展示搜索栏 */
const showSearchBar = ref<boolean>(true)
const searchForm = ref<DictTable>({
  status: '',
  initTime: [],
  path: '',
  type: '',
  name: ''
})

// 重置搜索框
function reSearch() {
  searchForm.value = {
    status: '',
    initTime: [],
    path: '',
    type: '',
    name: ''
  }
  getPageData()
}
/* 对话框控制 */
const dialogVisible = ref<boolean>(false)

/* 选择器 */
const selectData = ref<DictTable[]>([])
function onSelect(data: DictTable[]) {
  selectData.value = data
}
const delData = ref<DictTable[]>([])
/* 控制器 */
const controlType = ref<'none' | 'del' | 'add' | 'edit' | 'editKeyVal'>('none')
const dialogTitle = ref('')
// 删除控制
const Del = defineAsyncComponent(() => import('./Del.vue'))
function del(rows: DictTable[]) {
  controlType.value = 'del'
  dialogTitle.value = `你确定要删除以下${pageN}吗?`
  delData.value = rows
  dialogVisible.value = true
}
// 添加控制
const Add = defineAsyncComponent(() => import('./Add.vue'))
function add() {
  controlType.value = 'add'
  dialogTitle.value = `添加${pageN}`
  dialogVisible.value = true
}

//编辑控制
const Edit = defineAsyncComponent(() => import('./Edit.vue'))
const EditData = ref<DictTable>()
function edit(d: DictTable) {
  EditData.value = d
  controlType.value = 'edit'
  dialogTitle.value = `修改${pageN}`
  dialogVisible.value = true
}

// 编辑字典键值控制
const EditDictKeyVal = defineAsyncComponent(() => import('./EditDictKeyVal.vue'))
function editKeyVal(d: DictTable) {
  EditData.value = d
  controlType.value = 'editKeyVal'
  dialogTitle.value = `修改${pageN}`
  dialogVisible.value = true
}
</script>

<template>
  <div class="page-root">
    <div class="control">
      <div class="search" v-show="showSearchBar">
        <el-form :inline="true">
          <el-form-item class="date-picker" :label="tableDict?.get('initTime') || 'initTime'">
            <el-date-picker
              v-model="searchForm.initTime"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm:ss"
              date-format="YYYY/MM/DD ddd"
              time-format="HH:mm:ss"
              width="100px"
            />
          </el-form-item>
          <el-form-item :label="tableDict?.get('name') || 'name'">
            <el-input
              v-model="searchForm.name"
              :placeholder="'请输入' + tableDict?.get('name') || 'name'"
            ></el-input>
          </el-form-item>
          <el-form-item :label="tableDict?.get('path') || 'path'">
            <el-input
              v-model="searchForm.path"
              :placeholder="'请输入' + tableDict?.get('path') || 'path'"
            ></el-input>
          </el-form-item>
          <el-form-item :label="tableDict?.get('type') || 'type'">
            <el-input
              v-model="searchForm.type"
              :placeholder="'请输入' + tableDict?.get('type') || 'type'"
            ></el-input>
          </el-form-item>
          <el-form-item :label="tableDict?.get('status') || 'status'">
            <el-select
              v-model="searchForm.status"
              :placeholder="'请选择' + tableDict?.get('status') || 'status'"
            >
              <el-option label="无" value="" />
              <el-option :label="statusDict?.get('1') || '1'" value="1" />
              <el-option :label="statusDict?.get('0') || '0'" value="0" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" :icon="Search" @click="getPageData">搜索</el-button>
            <el-button :icon="Refresh" @click="reSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="ctl-bts">
        <span class="ctl-l">
          <AuthButton @click="add" auth-tag="sys:dict:add"></AuthButton>
          <AuthButton
            @click="edit(selectData[0])"
            :disabled="selectData.length != 1"
            auth-tag="sys:dict:edit"
          ></AuthButton>
          <AuthButton
            @click="del(selectData)"
            :disabled="selectData.length == 0"
            auth-tag="sys:dict:del"
          />
        </span>
        <span class="ctl-r">
          <el-button :icon="Search" circle @click="showSearchBar = !showSearchBar"></el-button>
          <el-button :icon="Refresh" @click="getPageData" circle></el-button>
        </span>
      </div>
    </div>
    <div class="ctx">
      <el-table
        v-loading="loading"
        table-layout="auto"
        :stripe="true"
        :data="page.rows"
        height="100%"
        :flexible="true"
        @selection-change="onSelect"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column prop="dId" align="center">
          <template #header>
            {{ tableDict?.get('dId') || 'dId' }}
          </template>
        </el-table-column>
        <el-table-column prop="name" align="center">
          <template #header>
            {{ tableDict?.get('name') || 'name' }}
          </template>
        </el-table-column>
        <el-table-column prop="type" align="center">
          <template #header>
            {{ tableDict?.get('type') || 'type' }}
          </template>
          <template #default="{ row }">
            <AuthLink auth-tag="sys:dict:edit_key_val" @click="editKeyVal(row)">{{
              row['type']
            }}</AuthLink>
          </template>
        </el-table-column>
        <el-table-column prop="path" align="center">
          <template #header>
            {{ tableDict?.get('path') || 'path' }}
          </template>
          <template #default="{ row }">
            <AuthLink auth-tag="sys:dict:edit_key_val" @click="editKeyVal(row)">{{
              row['path']
            }}</AuthLink>
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center">
          <template #header>
            {{ tableDict?.get('status') || 'status' }}
          </template>
          <template #default="{ row }">
            <el-tag :type="row['status'] === '1' ? 'success' : 'danger'">{{
              statusDict?.get(row['status']) || row['status']
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="initTime" align="center">
          <template #header>
            {{ tableDict?.get('initTime') || 'initTime' }}
          </template>
          <template #default="{ row }">
            {{ dateFormat(row?.initTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center">
          <template #header>
            {{ tableDict?.get('description') || 'description' }}
          </template>
        </el-table-column>
        <el-table-column align="center">
          <template #header> 操作 </template>
          <template #default="{ row }">
            <div class="buttons">
              <AuthLink auth-tag="sys:dict:del" @click="del([row])"></AuthLink>
              <AuthLink auth-tag="sys:dict:edit" @click="edit(row)"></AuthLink>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page="page" :get-page-data="getPageData" />
    </div>
    <el-dialog @closed="controlType = 'none'" v-model="dialogVisible" :title="dialogTitle">
      <Del
        v-if="controlType == 'del'"
        :refresh="getPageData"
        :table-head-dict-map="tableDict!"
        :rows="delData"
        @c-dlg="dialogVisible = false"
      />
      <Add
        v-else-if="controlType == 'add'"
        :refresh="getPageData"
        :d-map="tableDict!"
        @c-dlg="dialogVisible = false"
      />
      <Edit
        v-else-if="controlType == 'edit'"
        :refresh="getPageData"
        :d-map="tableDict!"
        :edit-data="EditData!"
        @c-dlg="dialogVisible = false"
      />

      <EditDictKeyVal
        v-else-if="controlType == 'editKeyVal'"
        :edit-data="EditData!"
        @c-dlg="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>
<style scoped lang="less">
@import '../../../assets/page_bgl.less';
</style>
